package hyit.app.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import hyit.app.dao.IAttendanceGroupDAO;
import hyit.app.model.AttendanceGroup;

public class AttendanceGroupDAOImpl implements IAttendanceGroupDAO {
	private Connection conn = null;
	private PreparedStatement pstmt = null;

	public AttendanceGroupDAOImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doCreate(AttendanceGroup attendanceGroup) throws Exception {
		boolean flag = false;
		String sql = "INSERT INTO attendance_group VALUES(NULL,?,?,?,?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, attendanceGroup.getSubjectNumber());
		this.pstmt.setInt(2, attendanceGroup.getDepartmentNumber());
		this.pstmt.setString(3, attendanceGroup.getName());
		this.pstmt.setLong(4, attendanceGroup.getLeaderId());
		if (this.pstmt.executeUpdate() > 0) {
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}

	@Override
	public boolean update(AttendanceGroup attendanceGroup) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public AttendanceGroup findBySubject(Integer number) throws Exception {
		AttendanceGroup attendanceGroup = null;
		String sql = "SELECT gid,subject_number,department_number,`name`,leaderid FROM attendance_group WHERE subject_number = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, number);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			attendanceGroup = new AttendanceGroup();
			attendanceGroup.setGid(rs.getInt("gid"));
			attendanceGroup.setSubjectNumber(rs.getInt("subject_number"));
			attendanceGroup.setDepartmentNumber(rs.getInt("department_number"));
			attendanceGroup.setName(rs.getString("name"));
			attendanceGroup.setLeaderId(rs.getLong("leaderid"));
		}
		this.pstmt.close();
		return attendanceGroup;
	}

	@Override
	public List<AttendanceGroup> findByDepartment(Integer number)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
